﻿using CommonUtils;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace Test.Core.Web.apis
{
    [SwaggerTag("文件接口")]
    [Route("api/[controller]/[action]")]
    public class FileController2 : ControllerBase
    {
        //static string FileRoot = "C:/FileRoot";
        static string FileRoot = "./FileRoot";

        [HttpPost]
        [SwaggerOperation("上传")]
        public ApiResult Upload([SwaggerParameter("文件")] IFormFile file)
        {
            FileUtil.SaveTo(FileRoot.Combine(file.Name), file.OpenReadStream());
            return ApiResult.Ok(file.Name);
        }

        [HttpGet("{path}")]
        [SwaggerOperation("下载")]
        public IActionResult Download([SwaggerParameter("路径")] string path)
        => File(FileUtil.OpenRead(FileRoot.Combine(path)), ContentType.Stream, path);

    }
}
